package com.chuantou.yuba.ui.fragment;

import java.util.HashMap;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;

import com.android.volley.Request.Method;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.chuantou.yuba.R;
import com.chuantou.yuba.adapter.FishingPlacesAdapter;
import com.chuantou.yuba.api.UriHelper;
import com.chuantou.yuba.api.request.BaseGsonRequest;
import com.chuantou.yuba.api.request.GsonRequest;
import com.chuantou.yuba.api.to.FishingPlaceDataTO;
import com.chuantou.yuba.api.to.FishingPlaceTO;
import com.chuantou.yuba.api.to.UserTO;
import com.chuantou.yuba.ui.PublishNearFishingActivity;
import com.chuantou.yuba.ui.widget.LoadingView.Style;
import com.chuantou.yuba.util.AccountManager;
import com.chuantou.yuba.util.Constants;

/**
 * 贡献中的钓场界面
 * 
 * @author Administrator
 * 
 */
public class ContributionFishingPlaceFragment extends PaginationListFragment<FishingPlaceDataTO> {
	private FishingPlacesAdapter mAdapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View contentView = super.onCreateView(inflater, container, savedInstanceState);
		if (mAdapter == null) {
			mAdapter = new FishingPlacesAdapter(mContext);
			mListView.setAdapter(mAdapter);
			mAdapter.setShowDistance(false);
			mAdapter.setPublic(false);
			mListView.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
					Object obj = parent.getItemAtPosition(position);
					if (obj != null && obj instanceof FishingPlaceTO) {
						FishingPlaceTO place = (FishingPlaceTO) obj;
						if (place.isActive()) {
							Intent intent = new Intent(getActivity(), PublishNearFishingActivity.class);
							intent.putExtra(Constants.EXTRA_TITLE, mContext.getString(R.string.fishing_place));
							intent.putExtra(Constants.EXTRA_DATA, PublishFishingPlaceFragment.class.getName());
							intent.putExtra(Constants.EXTRA_SHOP, place);
							startActivity(intent);
						} else {
							Toast.makeText(mContext, R.string.cannot_edit_inactive_fishing_place, Toast.LENGTH_SHORT).show();
						}
					}
				}
			});
			startRequest();
		}
		return contentView;
	}

	@Override
	protected BaseGsonRequest<FishingPlaceDataTO> getRequest(Listener<FishingPlaceDataTO> listener, ErrorListener errorListener) {
		String url = UriHelper.getContributionFishingPlaceUrl();
		HashMap<String, String> params = new HashMap<String, String>();
		params.put(UriHelper.PARAMS_PAGE_SIZE, String.valueOf(Constants.DEFAULT_PAGE_SIZE));
		params.put(UriHelper.PARAMS_PAGE_NO, String.valueOf(1));
		AccountManager accountManager = AccountManager.getInstance(mContext);
		UserTO user = accountManager.getUserTO();
		params.put(UriHelper.PARAMS_UID, String.valueOf(user.getUid()));
		params.put(UriHelper.PARAMS_TOKEN, user.getToken());
		GsonRequest<FishingPlaceDataTO> request = new GsonRequest<FishingPlaceDataTO>(Method.POST, url, listener, errorListener, params,
				FishingPlaceDataTO.class);
		return request;
	}

	@Override
	public void onResponse(FishingPlaceDataTO response) {
		if (getActivity() != null) {
			super.onResponse(response);
			if (response != null) {
				List<FishingPlaceTO> places = response.getPlaces();
				// 第一页
				if (response.getCurrentPage() <= 1) {
					// 保存到本地数据库缓存
					mAdapter.clear();
				}
				mAdapter.add(places);
			} else {
				mLoadingView.setStyle(Style.EMPTY, getEmptyString());
			}
			mAdapter.notifyDataSetChanged();
		}
	}

	@Override
	protected String getEmptyString() {
		return mContext.getString(R.string.no_content);
	}
}
